package com.zhiliang.waibao.myapplication.ui.login;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zhiliang.waibao.myapplication.R;
import com.zhiliang.waibao.myapplication.bean.LoginBean;
import com.zhiliang.waibao.myapplication.component.ApplicationComponent;
import com.zhiliang.waibao.myapplication.component.DaggerHttpComponent;
import com.zhiliang.waibao.myapplication.ui.MainActivity;
import com.zhiliang.waibao.myapplication.ui.base.AppManager;
import com.zhiliang.waibao.myapplication.ui.base.BaseActivity;
import com.zhiliang.waibao.myapplication.ui.login.contract.LoginContract;
import com.zhiliang.waibao.myapplication.ui.login.presenter.LoginPresenter;
import com.zhiliang.waibao.myapplication.utils.PreferenceUtil;
import com.zhiliang.waibao.myapplication.utils.T;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends BaseActivity<LoginPresenter> implements LoginContract.View {
    @BindView(R.id.login_password_text)
    TextView loginPasswordText;
    @BindView(R.id.login_password_ll)
    LinearLayout loginPasswordLl;
    @BindView(R.id.login_code_text)
    TextView loginCodeText;
    @BindView(R.id.login_code_ll)
    LinearLayout loginCodeLl;
    @BindView(R.id.login_phone01)
    TextView loginPhone01;
    @BindView(R.id.login_phone_edit)
    EditText loginPhoneEdit;
    @BindView(R.id.text001)
    TextView text001;
    @BindView(R.id.login_send_text)
    Button loginSendText;
    @BindView(R.id.lgoin_getcode_edit)
    EditText lgoinGetcodeEdit;
    @BindView(R.id.rigst)
    TextView rigst;
    @BindView(R.id.backpsd)
    TextView backpsd;
    @BindView(R.id.login_button)
    Button loginButton;
    @Override
    public void onRetry() {

    }

    @Override
    public int getContentLayout() {
        return R.layout.module_activity_login;
    }

    @Override
    public void initInjector(ApplicationComponent appComponent) {
        DaggerHttpComponent.builder()
                .applicationComponent(appComponent)
                .build()
                .inject(this);
    }

    @Override
    public void bindView(View view, Bundle savedInstanceState) {

    }

    @Override
    public void initData() {

    }

    @Override
    public void loadlogin(LoginBean loginBean) {
        hideLoadingDialog();
        if (loginBean.getStatus() == 1) {
            T.showShort(LoginActivity.this, loginBean.getMsg());
            PreferenceUtil.put("uids", loginBean.getData());
            startActivity(new Intent(LoginActivity.this, MainActivity.class));
            AppManager.getAppManager().finishActivity();
        } else {
            T.showShort(LoginActivity.this, loginBean.getMsg());
        }
    }

    @Override
    public void loadempe() {
        T.showShort(LoginActivity.this, "请填写手机号！");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.rigst, R.id.backpsd, R.id.login_button})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rigst:
//                startActivity(new Intent(LoginActivity.this, RegistActivity.class));
                break;
            case R.id.backpsd:
                break;
            case R.id.login_button:
                login();
                break;

        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();

    }
    private void login() {
        String tel = loginPhoneEdit.getText().toString().trim();
//        if (TextUtils.isEmpty(tel)) {
//            T.showShort(LoginActivity.this, "请填写手机号！");
//            return;
//        }
        String password = lgoinGetcodeEdit.getText().toString().trim();
//        if (TextUtils.isEmpty(password)) {
//            T.showShort(LoginActivity.this, "请填写密码或者验证码！");
//            return;
//        }
        if (mPresenter != null) {
            showLoadingDialog();
            mPresenter.login(tel, password);
        }
    }
}
